package com.power.using.mydaggerp.presenter.book;

import android.os.Bundle;
import android.support.annotation.NonNull;

import com.power.using.mydaggerp.constant.BundleKeyConstant;
import com.power.using.mydaggerp.contract.detail.BookDeatilContract;
import com.power.using.mydaggerp.model.bean.douban.book.BookDetailBean;
import com.power.using.mydaggerp.model.bean.douban.book.BookItemBean;
import com.power.using.mydaggerp.model.book.BookDetailModel;
import com.power.using.mydaggerp.ui.activity.detial.WebViewLoadActivity;

import io.reactivex.functions.Consumer;

/**
 * Created by 聪明一只哈 on 2019/10/18.
 * Time: 2019/10/18  10:41
 */
public class BookDetailPresenter extends BookDeatilContract.BookDetailPresenter{


    @NonNull
    public static BookDetailPresenter newInstance() {
        return new BookDetailPresenter();
    }


    @Override
    public void loadBookDetail(String id) {

        if(mIModel==null||mIView==null){
            return;
        }

        mRxManage.register(mIModel.getBookDetail(id).subscribe(new Consumer<BookDetailBean>() {
            @Override
            public void accept(BookDetailBean bean) throws Exception {
                if(mIView==null){
                    return;
                }

                mIView.showBookDetail(bean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if(mIView==null){
                    return;
                }

                mIView.showToast("Network error");
                mIView.showNerworkError();
            }
        }));
    }

    @Override
    public void onHeaderClick(BookItemBean bean) {
        Bundle bundle = new Bundle();
        bundle.putString(BundleKeyConstant.ARG_KEY_WEB_VIEW_LOAD_TITLE, bean.getTitle());
        bundle.putString(BundleKeyConstant.ARG_KEY_WEB_VIEW_LOAD_URL, bean.getAlt());
        mIView.startNewActivity(WebViewLoadActivity.class, bundle);

    }

    @Override
    protected BookDeatilContract.IBookDetailModel getModel() {
        return BookDetailModel.newInstance();
    }

    @Override
    public void onStart() {

    }
}
